package com.study.demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.flowable.engine.DynamicBpmnService;
import org.flowable.engine.FormService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ManagementService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:flowable-context.xml")
public class Demo07 {

    private ProcessEngine processEngine;
    private TaskService taskService;
    private RuntimeService runtimeService;
    private RepositoryService repositoryService;
    private HistoryService historyService;
    private DynamicBpmnService dynamicBpmnService;
    private FormService formService;
    private IdentityService identityService;
    private ManagementService managementService;
    private ProcessEngineConfiguration processEngineConfiguration;

    @Before
    public void testProcessEngine() {
        processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println("流程引擎类：" + processEngine);

        taskService = processEngine.getTaskService();
        System.out.println(taskService);

        runtimeService = processEngine.getRuntimeService();
        System.out.println(taskService);

        repositoryService = processEngine.getRepositoryService();
        System.out.println(repositoryService);

        historyService = processEngine.getHistoryService();
        System.out.println(historyService);

        dynamicBpmnService = processEngine.getDynamicBpmnService();
        System.out.println(dynamicBpmnService);

        formService = processEngine.getFormService();
        System.out.println(formService);

        identityService = processEngine.getIdentityService();
        System.out.println(identityService);

        managementService = processEngine.getManagementService();
        System.out.println(managementService);

        String name = processEngine.getName();
        System.out.println("流程引擎的名称： " + name);

        processEngineConfiguration = processEngine.getProcessEngineConfiguration();
        System.out.println(processEngineConfiguration);

    }

    /**
     * 关闭流程引擎
     */
    @After
    public void close(){
        processEngine.close();
    }

    /**
     * classpath方式部署
     * 涉及三张表：ACT_RE_PROCDEF,ACT_RE_DEPLOYMENT,ACT_GE_BYTEARRAY
     */
    @Test
    public void deploy(){
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment()
                .category("mutiInstanceOneVoteNo")
                .key("mutiInstanceOneVoteNo")
                .name("多实例一票否决")
                .addClasspathResource("多实例一票否决.bpmn20.xml");
        Deployment deploy = deploymentBuilder.deploy();

        System.out.println("多实例一票否决,流程ID: " + deploy.getId());
    }

    /**
     * 启动流程实例
     * 涉及表：ACT_RU_EXECUTION,ACT_RU_TASK
     */
    @Test
    public void startProcessInstanceByKey(){
        String processDefinitionKey = "mutiInstanceOneVoteNo";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);

        System.out.println("流程实例ID: " +processInstance.getId());
        System.out.println("活动实例ID: " +processInstance.getActivityId());


    }

    /**
     * 查询某个用户的任务
     */
    @Test
    public void queryUserTasks(){
        String assignee= "张三" ;
        String processDefinitionKey = "mutiInstanceOneVoteNo";
        List<Task> list = taskService.createTaskQuery()
                                        .taskAssignee(assignee)
                                        .processDefinitionKey(processDefinitionKey)
                                        .list();
        System.out.println("===============查询" + assignee + "的个人任务=============");
        for ( Task task: list) {


            System.out.println("任务ID: " +task.getId());
            System.out.println("执行实例的执行人: " +task.getAssignee());

            System.out.println("流程实例ID: " + task.getProcessInstanceId());
            System.out.println("流程实例定义ID: " +task.getProcessDefinitionId());
            System.out.println("执行实例ID: " +task.getExecutionId());
            System.out.println("任务名称: " +task.getName());
            System.out.println("任务定义key: " +task.getTaskDefinitionKey());
        }

    }

    /**
     * 完成个人任务:依次审批，对于串行任务节点
     */
    @Test
    public void complete(){
        String taskId = "27504" ;
        taskService.complete(taskId);

    }
    /**
     * 一票通过
     */
    @Test
    public void oneVotePass(){
        String taskId = "32516" ;
        Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("onePass", true); 
        taskService.complete(taskId,variables);

    }
    /**
     * 一票否决
     */
    @Test
    public void oneVoteNo(){
    	String taskId = "42503" ;
    	
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("oneReject", true); 
        taskService.complete(taskId,variables);

    }


}
